package at.fhooe.stefanirndorfer.entity;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.LWJGLException;

import at.fhooe.stefanirndorfer.collision.CollisionDetector;
import at.fhooe.stefanirndorfer.walls.Wall;

public class EntityManager {

	List<Entity> entities;
	
	public EntityManager(){
		entities = new ArrayList<Entity>();
	}
	
	public synchronized void updateAll(int delta){
		for(Entity e : entities){
			e.update(delta);
		}
	} 
	
	public synchronized void addEntity(Entity e){
		entities.add(e);
	}
	
	public synchronized void removeEntity(Entity e){
		for(int i = 0; i < entities.size(); i++){
			if(entities.get(i).equals(e)){
				Entity ent = entities.get(i);
				try {
					ent.tearDownVisual(ent.getId());
				} catch (LWJGLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				entities.remove(i);
				
				System.out.println("remove Entity");
				break;
			}
		}
	}

	public synchronized Wall checkWallCollisions(int u, int v) {
		for(Entity e : entities){
			if(e instanceof Wall){
				if(CollisionDetector.hasCollisionPointRect(e, u, v)){
					return (Wall)e;
				}
			}
		}
		return null;
	}

	public void removeAll() {
		for (int i = entities.size()-1; i >= 0; i--) {
			entities.remove(i);
		}
		
	}
	
}
